/***********************************************************************************
Name:  Casedetailpagecontroller
Copyright © 2012  Lodha Group
===================================================================================
===================================================================================
Purpose:
-------
Controller for Casedetailpage VF page for customer portal users
===================================================================================
===================================================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          20/05/2012      INITIAL DEVELOPMENT
************************************************************************************/
public class Casedetailpagecontroller {

 
    public String caseId{get;set;}
    public String accountId{get;set;}
    public String accountname{get;set;}
    public Case caselist{get;set;}
    public List<CaseHistory> casehistorylist{get;set;}
    public List<Task> tasklist{get;set;}
    public List<Casehistorywrapper> wrapperlist{get;set;}
    public List<CaseComment> commentlist{get;set;}
    public List<Casecommentwrapper> casecommentwrapperlist{get;set;}
    //public List<Taskwrapper> taskwrapperlist{get;set;}
    public Boolean editmode{get;set;}
    public Boolean displaymode{get;set;}
    public Boolean Isstatus{get;set;}
    public Boolean Isadf{get;set;}
    public Boolean Isalladf{get;set;}
    public Boolean Iscrmpick{get;set;}
    public Boolean ispdpick{get;set;}
    public Boolean Isbankdetail{get;set;}
    public String commentbody{get;set;}
    public Boolean displayformone{get;set;}
    public Boolean displayformtwo{get;set;}
    public Boolean isinteractionpick{get;set;}
    public Boolean issanctionpick{get;set;}
    public Boolean isregistration{get;set;}
    public Boolean ispdcompleted{get;set;}
    public Boolean isdemandpick{get;set;}
    public Boolean Isdisbursement{get;set;}
    public Boolean Isnotdisbursement{get;set;}
   
    
    public Casedetailpagecontroller(ApexPages.StandardController controller) {
        Initmethod();
    }
       
       
    public void Initmethod(){
    
        editmode=false;
        displaymode=true;
        Isbankdetail=false;
        displayformone=true;
        displayformtwo=false;
        isinteractionpick=false;
        issanctionpick=false;
        isregistration=false;
        ispdcompleted=false;
        isdemandpick=false;        
        caseId=ApexPages.currentPage().getParameters().get('id');
                           
        Casedetail();                              
        Casehistorydetail();
        Casecommentdetails();
        //Activityhistory();
    }   
       
    public void Casedetail()
    {
        caselist=[select id,CaseNumber,owner.Name,Status,Loan_Stages__c,Interactions__c,Completed__c,Registration__c,
        Sanction__c,Remarks_1__c,Remarks_2__c,Account.Name,Account.Id,Remarks__c,Tele_caller__c,Manager__c,
        Cheque_Received__c,Cheque_received_Date__c,Amount__c,PD__c,CRM__c,ADF__c,         
        Loan_amount__c,Name_of_bank__c,Branch__c,Date_Of_Login__c,Loan_Account_No__c,Query_Type__c,Sub_Type__c,
        Additional_Comments__c,CreatedBy.Name,LastModifiedBy.Name,
        (SELECT CreatedDate, Field, OldValue, NewValue FROM Histories) 
        from Case where id=:caseId];
        
             accountId=caselist.Account.Id;
             accountname=caselist.Account.Name;
                       
            if(caselist.Loan_Stages__c!=null&&caselist.Loan_Stages__c!='')
        {
            Isstatus=true;
        }else{
            Isstatus=true;
        }
        if(caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Sanction'||caselist.Loan_Stages__c=='In Process'||caselist.Loan_Stages__c=='ADF')
         {
             Isadf=true;
         }else{
             Isadf=false;
         }
        
        if(caselist.Loan_Stages__c=='Lodha Login'||caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Disbursement')
         {
            Isbankdetail=true;
            if(caselist.Loan_Stages__c=='Disbursement'){
              Isdisbursement=true;
              Isnotdisbursement=false;
             }else{
               Isdisbursement=false;
               Isnotdisbursement=true;
             }
            
         }else{         
           Isbankdetail=false;
         }         
       if(caselist.Loan_Stages__c=='Initial Call'||caselist.Loan_Stages__c=='Appointment'||caselist.Loan_Stages__c=='In Process')
         {
           Isinteractionpick=true;
               
         }else{
             Isinteractionpick=false;
         }
        if(caselist.Loan_Stages__c=='Sanction')
           {
             
             Issanctionpick=true;
           }else{
               Issanctionpick=false;
           }
        if(caselist.Loan_Stages__c=='Registration')
          {
           
            Isregistration=true;
          }else{
            Isregistration=false;
          }
        if(caselist.Loan_Stages__c=='In Process')
          {
             Ispdcompleted=true;
          }else{
             Ispdcompleted=false;
          }
        if(caselist.Loan_Stages__c=='Demand')
          {
              Isdemandpick=true;
          }else{
              Isdemandpick=false;
          }
        if(caselist.Loan_Stages__c=='Initial Call')
          {
              Iscrmpick=true;
          }else{
              Iscrmpick=false;
          } 
        if(caselist.Loan_Stages__c=='Customer Hold'||caselist.Loan_Stages__c=='In Process')
         {
              ispdpick=true;
          }else{
              ispdpick=false;
          }
        if(caselist.Loan_Stages__c=='ADF')
          {
              Isalladf=true;
          }else{
              Isalladf=false;
          }
                                 
    }
    public PageReference submitcase()
    {        
        caselist.AccountId=accountid;
        update caselist;
        
        editmode=false;
        displaymode=true;
        if(caselist.Loan_Stages__c!=null&&caselist.Loan_Stages__c!='')
        {
            Isstatus=true;
        }else{
            Isstatus=true;
        }
        if(caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Sanction'||caselist.Loan_Stages__c=='In Process'||caselist.Loan_Stages__c=='ADF')
         {
             Isadf=true;
         }else{
             Isadf=false;
         }
        
        if(caselist.Loan_Stages__c=='Lodha Login'||caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Disbursement')
         {
            Isbankdetail=true;
            if(caselist.Loan_Stages__c=='Disbursement'){
              Isdisbursement=true;
              Isnotdisbursement=false;
             }else{
               Isdisbursement=false;
               Isnotdisbursement=true;
             }
            
         }else{         
           Isbankdetail=false;
         }         
       if(caselist.Loan_Stages__c=='Initial Call'||caselist.Loan_Stages__c=='Appointment'||caselist.Loan_Stages__c=='In Process')
         {
           Isinteractionpick=true;
               
         }else{
             Isinteractionpick=false;
         }
        if(caselist.Loan_Stages__c=='Sanction')
           {
             
             Issanctionpick=true;
           }else{
               Issanctionpick=false;
           }
        if(caselist.Loan_Stages__c=='Registration')
          {
           
            Isregistration=true;
          }else{
            Isregistration=false;
          }
        if(caselist.Loan_Stages__c=='In Process')
          {
             Ispdcompleted=true;
          }else{
             Ispdcompleted=false;
          }
        if(caselist.Loan_Stages__c=='Demand')
          {
              Isdemandpick=true;
          }else{
              Isdemandpick=false;
          }
        if(caselist.Loan_Stages__c=='Initial Call')
          {
              Iscrmpick=true;
          }else{
              Iscrmpick=false;
          } 
        if(caselist.Loan_Stages__c=='Customer Hold'||caselist.Loan_Stages__c=='In Process')
         {
              ispdpick=true;
          }else{
              ispdpick=false;
          }
        if(caselist.Loan_Stages__c=='ADF')
          {
              Isalladf=true;
          }else{
              Isalladf=false;
          }
        return null;
    }
    public void Iseditmode()
    {
        editmode=true;
        displaymode=false;
        
    }
  

  
    public PageReference Onchange()
     {
        editmode=true;
        displaymode=false;
        if(caselist.Loan_Stages__c!=null&&caselist.Loan_Stages__c!='')
        {
            Isstatus=true;
        }else{
            Isstatus=true;
        }
        if(caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Sanction'||caselist.Loan_Stages__c=='In Process'||caselist.Loan_Stages__c=='ADF')
         {
             Isadf=true;
         }else{
             Isadf=false;
         }
        
        if(caselist.Loan_Stages__c=='Lodha Login'||caselist.Loan_Stages__c=='Bank Login'||caselist.Loan_Stages__c=='Disbursement')
         {
            Isbankdetail=true;
            if(caselist.Loan_Stages__c=='Disbursement'){
              Isdisbursement=true;
              Isnotdisbursement=false;
             }else{
               Isdisbursement=false;
               Isnotdisbursement=true;
             }
            
         }else{         
           Isbankdetail=false;
         }         
       if(caselist.Loan_Stages__c=='Initial Call'||caselist.Loan_Stages__c=='Appointment'||caselist.Loan_Stages__c=='In Process')
         {
           Isinteractionpick=true;
               
         }else{
             Isinteractionpick=false;
         }
        if(caselist.Loan_Stages__c=='Sanction')
           {
             
             Issanctionpick=true;
           }else{
               Issanctionpick=false;
           }
        if(caselist.Loan_Stages__c=='Registration')
          {
           
            Isregistration=true;
          }else{
            Isregistration=false;
          }
        if(caselist.Loan_Stages__c=='In Process')
          {
             Ispdcompleted=true;
          }else{
             Ispdcompleted=false;
          }
        if(caselist.Loan_Stages__c=='Demand')
          {
              Isdemandpick=true;
          }else{
              Isdemandpick=false;
          }
        if(caselist.Loan_Stages__c=='Initial Call')
          {
              Iscrmpick=true;
          }else{
              Iscrmpick=false;
          } 
        if(caselist.Loan_Stages__c=='Customer Hold'||caselist.Loan_Stages__c=='In Process')
         {
              ispdpick=true;
          }else{
              ispdpick=false;
          }
        if(caselist.Loan_Stages__c=='ADF')
          {
              Isalladf=true;
          }else{
              Isalladf=false;
          }
          
        caselist.Interactions__c=null;
         return null;
     }
    public PageReference Cancel()
     {
         editmode=false;
         displaymode=true;
         displayformone=true;
         displayformtwo=false;
         Initmethod();
         return null;
     }
     
   public PageReference CaseComment()
     {
        displayformone=false;
        displayformtwo=true;
         return null;
     }
     public class Casehistorywrapper{         
         public DateTime createddate{get;set;}
         public String username{get;set;}
         public String field{get;set;}
         public String newvalue{get;set;}
    }
    public void Casehistorydetail(){
    
        casehistorylist=[Select CreatedDate, Field,CreatedBy.Name, OldValue, NewValue FROM CaseHistory where Case.Id=:caseid];         
        wrapperlist=new List<Casehistorywrapper>();    
        for(CaseHistory ch: casehistorylist){
        
            Casehistorywrapper wrapper=new Casehistorywrapper();
            String newval=(String)ch.NewValue;            
            if(ch.Field=='Created'){        
                wrapper.createddate=ch.CreatedDate;                        
                wrapper.field='Created';
                wrapper.username=ch.CreatedBy.Name;       
                wrapperlist.add(wrapper);
            }
           else if(!newval.Contains('00'))
              {
                  wrapper.createddate=ch.CreatedDate;
                  wrapper.field=ch.Field;
                  wrapper.username=ch.CreatedBy.Name;
                  wrapper.newvalue=(String)ch.Newvalue; 
                  wrapperlist.add(wrapper);
              }
        }
    }
    
   public PageReference submitcomment(){
      
       CaseComment comment=new CaseComment();
       comment.CommentBody=commentbody;
       comment.ParentId=caseid;
       comment.IsPublished=true;
       insert comment;
       
         
         Initmethod();      
       return null;
   }
   public class Casecommentwrapper{         
         public DateTime createddate{get;set;}
         public String username{get;set;}
         public String commenttext{get;set;}
        
    }
   public void Casecommentdetails(){
       commentlist=[select CommentBody,CreatedBy.Name,CreatedDate from CaseComment where Parent.Id=:caseid];
       casecommentwrapperlist=new List<Casecommentwrapper>();
       
       for(CaseComment comment: commentlist){
           Casecommentwrapper wrapper=new Casecommentwrapper();
           wrapper.createddate=comment.CreatedDate;
           wrapper.username=comment.CreatedBy.Name;
           wrapper.commenttext=comment.CommentBody;
           casecommentwrapperlist.add(wrapper);
       }
       
   }
 
 /*  
   public PageReference Logacall(){
   
       PageReference reference=new PageReference('/apex/CreateCaseActivity?id='+caseId);
       reference.setredirect(true);
       return reference;
   
   }
   
   public class Taskwrapper{
   
        public String whoname{get;set;}
        public String whatname{get;set;}
        public Date duedate{get;set;}
        public Date lastmodifieddate{get;set;}
        public String ownername{get;set;} 
   }
   
   public void Activityhistory(){
   
       taskwrapperlist=new List<Taskwrapper>();
       tasklist=[select Who.Name,What.Name,ActivityDate,Owner.Name,LastModifiedDate from Task where What.id=:caseId];
       
       for(Task ta: tasklist){  
            
           Taskwrapper wrapper=new Taskwrapper();
                      
           wrapper.whoname=ta.Who.Name;
           wrapper.whatname=ta.What.Name;
           wrapper.duedate=ta.ActivityDate;
           wrapper.ownername=ta.Owner.Name;
           wrapper.lastmodifieddate=ta.LastModifiedDate.date();
           taskwrapperlist.add(wrapper);             
       }
       
       
   }*/
   
   public String inputvalue{get;set;}
   public String Error{get;set;}
   public List<Account> accountfilterlist{get;set;}
   public Boolean showcolumn {get; set;}
   public List<accountwrapper> accwrapperlist{get;set;}
    public class accountwrapper{    
        public String Accountid{get;set;}
        public String AccountName{get;set;}
    }
    public PageReference runQuery()
        {
        
            
            String Querystring=null;

              String recordtype='Booked Account';
              if(inputvalue!=null && inputvalue!=''){
                inputvalue='%'+inputvalue+'%';                                
                     Querystring='Select Id,Name,FirstName,LastName from Account where (Firstname LIKE '+'\''+inputvalue+'\''+' OR LastName Like '+'\''+inputvalue+'\''+') AND(Account.RecordType.Name='+'\''+recordtype+'\''+') LIMIT 100';
                }else{
                       Querystring='Select Id,Name,FirstName,LastName from Account where Account.RecordType.Name='+'\''+recordtype+'\' LIMIT 100';
                    }
                accountfilterlist=Database.Query(Querystring); 
                
             if(!accountfilterlist.isEmpty()){
                 showcolumn=true; 
                  accwrapperlist=new List<accountwrapper>();
                  for(Account acc: accountfilterlist)
                    {                            
                        accountwrapper wrapper=new accountwrapper();
                        wrapper.Accountid=acc.Id;
                        wrapper.AccountName=acc.Name;
                        accwrapperlist.add(wrapper);            
                    }  
                 }                                                                     
          
           inputvalue=inputvalue.replace('%',''); 
            return null;
        }
        
     public static testmethod void testCasedetailpagecontroller () {
     
     
         Account a=new Account();
         a.RecordTypeId=[Select id from RecordType where SobjectType='Account' AND Name='Booked Account' Limit 1].Id;
         a.FirstName='Test';
         a.LastName='Account';
         insert a;
         
         Case c=new Case();
         c.AccountId=a.id;
         insert c;
         
        
         
        ApexPages.currentPage().getParameters().put('id',c.id);
        
        ApexPages.StandardController stdcontroller = new ApexPages.StandardController(new Case());
        Casedetailpagecontroller controller = new Casedetailpagecontroller(stdcontroller);
        controller.caselist.Loan_Stages__c='Disbursement';
        controller.inputvalue='Test';
        controller.Iseditmode();
        controller.submitcase();
        controller.Onchange();
        controller.Cancel();
        controller.CaseComment();
        controller.Casehistorydetail();
        controller.submitcomment();
        controller.runQuery();
        
      }
}